﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.Equipment.common
{
    public partial class recipientsapplist : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("JCSQSH");//必须写在ispostback外
            strPageUserType.Add("mana");
            strPageUserType.Add("teacher");

            if (!Page.IsPostBack)
            {
                tbEType.Text = "";
                string strwhere = "1=1";
                if (UserType == "teacher")
                {
                    strwhere += " and MRecipients = '"+ UserRealName +"'";
                }
                strwhere += " order by ID desc";
                if (Request.QueryString["Type"] != null)
                {
                    Label1.Visible = false;
                    ddlEType.Visible = false;
                    tbUPerson.Visible = true;
                    strwhere = " Type='" + Request.QueryString["Type"].ToString() + "' and " + strwhere;
                    tbEType.Text = Request.QueryString["Type"].ToString();
                }
                else
                {
                    Label1.Visible = true;
                    ddlEType.Visible = true;
                    tbUPerson.Visible = true;                    
                }
                bindddl();
                bind(strwhere);
                IniPage();
            }
        }

        public void bind(string strWhere)
        {
            lbErr.Visible = false;
            List<Model.RecipientsApp> listrescipientsappmodule = new List<Model.RecipientsApp>();
            ExtendBLL.RecipientsApp berescipients = new ExtendBLL.RecipientsApp();
            listrescipientsappmodule = berescipients.GetModelList(strWhere);

            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();


            try
            {
                foreach (Model.RecipientsApp rapp in listrescipientsappmodule)
                {
                    mLab = blab.GetModelList("Lab_Code = '" + rapp.EncodedWarehouse + "'");
                    if (mLab.Count > 0)
                    {
                        rapp.EncodedWarehouse = mLab[0].Lab_Name;
                    }
                    else
                    {
                        rapp.EncodedWarehouse = "无";
                    }
                    mLab = blab.GetModelList("Lab_Code = '" + rapp.Laboratory + "'");
                    if (mLab.Count > 0)
                    {
                        rapp.Laboratory = mLab[0].Lab_Name;
                    }
                    else
                    {
                        rapp.Laboratory = "无";
                    }
                    rapp.Type = LabMS.Common.Global.GetEquipmentByCode(rapp.Type);
                    rapp.State = LabMS.Common.Status.GetOpenProjectStatus(rapp.State);
                }
            }
            catch(Exception ex)
            {
                lbErr.Text = ex.Message;
                lbErr.Visible = true;
                return;
            }

            GridViewrapp.DataSource = listrescipientsappmodule;
            GridViewrapp.DataBind();
        }
        public void IniPage()
        {
            tbTime1.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
            tbTime2.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");

            tbTime1.Attributes.Add("readOnly", "-1");
            tbTime2.Attributes.Add("readOnly", "-1");

            TextBox1.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
            TextBox2.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");

            TextBox1.Attributes.Add("readOnly", "-1");
            TextBox2.Attributes.Add("readOnly", "-1");
        }

        public void bindddl()
        {
            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
            mLab = blab.GetModelList(" Lab_ParentID is null ");
            ddlWarehouse.DataSource = mLab;
            ddlWarehouse.DataTextField = "Lab_Name";
            ddlWarehouse.DataValueField = "Lab_Code";
            ddlWarehouse.DataBind();
            ddlWarehouse.Items.Insert(0, new ListItem("", "null"));
        }

        protected void GridViewrapp_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            GridViewrapp.PageIndex = e.NewPageIndex;
            if (lbSearch.Text == "0")
            {
                string strwhere = "1=1 order by ID desc";
                string strwhere2 = " 1=1 ";
                if (tbEType.Text != "")
                {
                    strwhere = "  and  Type='" + tbEType.Text + "' and " + strwhere;
                    if (UserType == "teacher")
                    {
                        strwhere2 += " and  MRecipients = '" + UserRealName + "'";
                    }
                    strwhere = strwhere2 + strwhere;
                }

                bind(strwhere);
            }
            else
            {
                string strWhere = " 1=1 ";
                if (!tbCName.Text.Trim().Equals(""))
                {
                    strWhere += " Name like '%" + tbCName.Text.Trim() + "%' ";
                }
                if (!tbUPerson.Text.Trim().Equals(""))
                {
                    strWhere += " MRecipients like '%" + tbUPerson.Text.Trim() + "%' ";
                }
                if (!ddlWarehouse.SelectedValue.Equals("null"))
                {
                    strWhere += " and  EncodedWarehouse = '" + ddlWarehouse.SelectedValue + "'";
                }
                strWhere += " and  Type = '" + ddlEType.SelectedValue + "'";
                if (!tbTime1.Text.Trim().Equals(""))
                {
                    strWhere += " and AppTime >= '" + tbTime1.Text.Trim() + "'";
                }
                if (!tbTime2.Text.Trim().Equals(""))
                {
                    DateTime dtTemp = new DateTime();
                    if (!DateTime.TryParse(tbTime2.Text.Trim(), out dtTemp))
                    {
                        dtTemp = DateTime.Today;
                    }
                    else
                    {
                        dtTemp.AddDays(1.0);
                    }
                    strWhere += " and AppTime <= '" + dtTemp.ToShortDateString() + "'";
                }
                if (!TextBox1.Text.Trim().Equals(""))
                {
                    strWhere += " and RecipientTime >= '" + TextBox1.Text.Trim() + "'";
                }
                if (!TextBox2.Text.Trim().Equals(""))
                {
                    DateTime dtTemp = new DateTime();
                    if (!DateTime.TryParse(TextBox2.Text.Trim(), out dtTemp))
                    {
                        dtTemp = DateTime.Today;
                    }
                    else
                    {
                        dtTemp.AddDays(1.0);
                    }
                    strWhere += " and RecipientTime <= '" + dtTemp.ToShortDateString() + "'";
                }
                if (ddlState.SelectedValue != "-1")
                {
                    strWhere += " and State = '" + ddlState.SelectedValue + "'";
                }
                if (UserType == "teacher")
                {
                    strWhere += " and  MRecipients = '" + UserRealName + "'";
                }
                bind(strWhere + "order by ID desc");
            }

        }

        protected void GridViewrapp_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                e.Row.Attributes["style"] = "cursor:pointer";
                int itemp = e.Row.Cells.Count;
                for (int i = 0; i < itemp; i++)
                {
                    if (tbEType.Text == "")
                    {
                        e.Row.Cells[i].Attributes.Add("OnDblClick", "window.location.href='recipientappperview.aspx?ID="
                                           + GridViewrapp.DataKeys[GridViewrapp.DataKeys.Count - 1].Value.ToString() + "'");
                    }
                    if (tbEType.Text != "")
                    {
                        e.Row.Cells[i].Attributes.Add("OnDblClick", "window.location.href='recipientappperview.aspx?ID="
                                           + GridViewrapp.DataKeys[GridViewrapp.DataKeys.Count - 1].Value.ToString() + "&Type=" + tbEType.Text + "'");
                    }
                }
            }
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            lbSearch.Text = "1";
            string strWhere = " 1=1 ";
            if (!tbCName.Text.Trim().Equals(""))
            {
                strWhere += " Name like '%" + tbCName.Text.Trim() + "%' ";
            }
            if (!tbUPerson.Text.Trim().Equals(""))
            {
                strWhere += " MRecipients like '%" + tbUPerson.Text.Trim() + "%' ";
            }
            if (!ddlWarehouse.SelectedValue.Equals("null"))
            {
                strWhere += " and  EncodedWarehouse = '" + ddlWarehouse.SelectedValue + "'";
            }
            strWhere += " and  Type = '" + ddlEType.SelectedValue + "'";
            if (!tbTime1.Text.Trim().Equals(""))
            {
                strWhere += " and AppTime >= '" + tbTime1.Text.Trim() + "'";
            }
            if (!tbTime2.Text.Trim().Equals(""))
            {
                DateTime dtTemp = new DateTime();
                if (!DateTime.TryParse(tbTime2.Text.Trim(), out dtTemp))
                {
                    dtTemp = DateTime.Today;
                }
                else
                {
                    dtTemp.AddDays(1.0);
                }
                strWhere += " and AppTime <= '" + dtTemp.ToShortDateString() + "'";
            }
            if (!TextBox1.Text.Trim().Equals(""))
            {
                strWhere += " and RecipientTime >= '" + TextBox1.Text.Trim() + "'";
            }
            if (!TextBox2.Text.Trim().Equals(""))
            {
                DateTime dtTemp = new DateTime();
                if (!DateTime.TryParse(TextBox2.Text.Trim(), out dtTemp))
                {
                    dtTemp = DateTime.Today;
                }
                else
                {
                    dtTemp.AddDays(1.0);
                }
                strWhere += " and RecipientTime <= '" + dtTemp.ToShortDateString() + "'";
            }
            if (ddlState.SelectedValue != "-1")
            {
                strWhere += " and State = '" + ddlState.SelectedValue + "'";
            }
            bind(strWhere + "order by ID desc");
        }

        protected void btReset_Click(object sender, EventArgs e)
        {
            string strwhere = "1=1 order by ID desc";
            if (tbEType.Text != "")
            {
                strwhere = " Type='" + tbEType.Text + "' and " + strwhere;
            }
            
            bindddl();
            bind(strwhere);
            IniPage();

            tbCName.Text = "";
            tbTime1.Text = "";
            tbTime2.Text = "";
            tbUPerson.Text = "";
            TextBox1.Text = "";
            TextBox2.Text = "";
            ddlEType.SelectedIndex = 1;
            ddlWarehouse.SelectedIndex = 1;
            ddlState.SelectedIndex = 1;
            lbSearch.Text = "0";
        }
    }
}
